package localizer.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SimRegisterActivity extends Activity {
	Config config;
	String username;
	String password;
	boolean sucess=false;
	String emergencyText = getString(R.string.emergencyText);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sim_confirm_layout);
		config = Config.getInstance(this);
		password = config.getPassword(this);
		username= config.getUsername(this);
		Button okButton = (Button) findViewById(R.id.buttonOkSim);
		Button cancelButton = (Button) findViewById(R.id.buttonCancelSim);
		TelephonyManager tel = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        final String simId = tel.getSimSerialNumber();
		okButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String userText = ((TextView)SimRegisterActivity.this.findViewById(R.id.textLoginSim)).getText().toString();
				String passwordText = ((TextView)SimRegisterActivity.this.findViewById(R.id.textPasswordSim)).getText().toString();
				if(username.equals(userText)&&password.equals(passwordText)){					
					config.addSimId(SimRegisterActivity.this, simId);
					sucess=true;
					Toast.makeText(SimRegisterActivity.this, getString(R.string.sim_registred), Toast.LENGTH_SHORT).show();
					finish();
				}else{
					Toast.makeText(SimRegisterActivity.this, getString(R.string.sim_not_registred), Toast.LENGTH_SHORT).show();
					finish();
				}
			}
		});
		
		cancelButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(SimRegisterActivity.this);
				builder.setMessage(getString(R.string.confirm_cancel_sim))
				       .setCancelable(false)
				       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                SimRegisterActivity.this.finish();
				           }
				       })
				       .setNegativeButton("No", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(!sucess){
			sendEmergencySMS();
		}
	}
	private void sendEmergencySMS(){
		SmsManager sms = SmsManager.getDefault();
		String phoneNumer=null;
		try {
			
			phoneNumer=config.getEmergencyPhone(this);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(phoneNumer!=null && !phoneNumer.equals("")){
			sms.sendTextMessage(phoneNumer, null, emergencyText, null, null);
		}
		
	}

}
